import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode : 90. 子集 II
 * @author: WXY
 * @create: 2023-01-19 10:57
 * @Version 1.0
 **/
public class Num090_subsetsWithDup {
    public static List<List<Integer>> ans = new ArrayList<>();
    public static List<Integer> path = new ArrayList<>();
    public static List<List<Integer>> subsetsWithDup(int[] nums) {
        boolean[] used = new boolean[nums.length];
        Arrays.sort(nums);
        ans.add(new ArrayList<>());
//        process1(nums, 0,used);
        process(nums, 0);
        return ans;
    }

    private static void process(int[] nums, int index) {
        if (index == nums.length) {
            return;
        }
        boolean[] used = new boolean[21];
        for (int i = index; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && used[nums[i] + 10] == true) continue;
            path.add(nums[i]);
            used[nums[i] + 10] = true;
            ans.add(new ArrayList<>(path));
            process(nums, i + 1);
            path.remove(path.size() - 1);
        }
    }

    private static void process1(int[] arr, int index, boolean[] used) {
        ans.add(new ArrayList<>(path));
        if (index == arr.length) {
            return;
        }
        for (int i = index; i < arr.length; i++) {
            if (i > 0 && arr[i] == arr[i - 1] && used[i - 1] == false)continue;
            path.add(arr[i]);
            used[i] = true;
            process1(arr, i + 1, used);
            used[i] = false;
            path.remove(path.size() - 1);
        }
    }

    public static void main(String[] args) {
        int[] arr = {2,2,2, 2,2,2,2,2, 2};
        List<List<Integer>> list = subsetsWithDup(arr);
        System.out.println(list);
    }
}
